<template>
  <div class="finance-manager">
    <!-- 页面标题区域 -->
    <div class="page-header">
      <h2>融资申请管理</h2>
      <el-tag type="info">共 {{ total }} 条申请</el-tag>
    </div>

    <!-- 搜索和操作区域 -->
    <el-card class="operation-area" shadow="hover">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" placeholder="请输入用户名" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="全部" value=""></el-option>
            <el-option label="待审核" value="PENDING"></el-option>
            <el-option label="已通过" value="APPROVED"></el-option>
            <el-option label="已拒绝" value="REJECTED"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="medium" plain type="primary" @click="handleSearch">查询</el-button>
          <el-button size="medium" plain @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="operation-buttons">
        <el-button plain size="medium" icon="el-icon-refresh" @click="getList">刷新</el-button>
      </div>
    </el-card>

    <!-- 融资申请列表 -->
    <el-card class="table-card" shadow="hover">
      <el-table :data="filteredList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="username" label="用户名" width="120"></el-table-column>
        <el-table-column prop="amount" label="金额" width="120">
          <template slot-scope="scope">
            <span style="color:#43a047;font-weight:600">¥{{ scope.row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="reason" label="说明"></el-table-column>
        <el-table-column prop="status" label="状态" width="120">
          <template slot-scope="scope">
            <el-tag :type="statusTagType(scope.row.status)">
              {{ statusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="applyTime" label="申请时间" width="180">
          <template slot-scope="scope">
            {{ formatTime(scope.row.applyTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="approveTime" label="审核时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.approveTime ? formatTime(scope.row.approveTime) : '-' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button type="success" size="small" :disabled="scope.row.status !== 'PENDING'" @click="approve(scope.row)">通过</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.currentPage" :limit.sync="queryParams.size" @pagination="getList" />
    </el-card>
    <!-- 装饰图片 -->
    <img src="@/assets/imgs/img2.png" class="finance-decoration" alt="leaf" />
  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination/index.vue'

export default {
  name: 'FinanceManager',
  components: { Pagination },
  data() {
    return {
      searchForm: {
        username: '',
        status: ''
      },
      queryParams: {
        currentPage: 1,
        size: 10
      },
      list: [],
      total: 0
    }
  },
  computed: {
    filteredList() {
      let arr = this.list
      if (this.searchForm.username) {
        arr = arr.filter(item => item.username && item.username.includes(this.searchForm.username))
      }
      if (this.searchForm.status) {
        arr = arr.filter(item => item.status === this.searchForm.status)
      }
      // 分页
      const start = (this.queryParams.currentPage - 1) * this.queryParams.size
      return arr.slice(start, start + this.queryParams.size)
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      try {
        const res = await request.get('/finance/all')
        if (Array.isArray(res)) {
          this.list = res.sort((a, b) => b.id - a.id)
          this.total = this.list.length
        }
      } catch (e) {
        this.$message.error('获取数据失败')
      }
    },
    handleSearch() {
      this.queryParams.currentPage = 1
    },
    resetSearch() {
      this.searchForm = { username: '', status: '' }
      this.handleSearch()
    },
    async approve(row) {
      try {
        await request.post(`/finance/approve/${row.id}`)
        this.$message.success('审核通过')
        this.getList()
      } catch (e) {
        this.$message.error('操作失败')
      }
    },
    statusText(status) {
      switch (status) {
        case 'PENDING': return '待审核'
        case 'APPROVED': return '已通过'
        case 'REJECTED': return '已拒绝'
        default: return status
      }
    },
    statusTagType(status) {
      switch (status) {
        case 'PENDING': return 'warning'
        case 'APPROVED': return 'success'
        case 'REJECTED': return 'danger'
        default: return 'info'
      }
    },
    formatTime(time) {
      if (!time) return '-'
      const d = new Date(time)
      return d.toLocaleString()
    }
  }
}
</script>

<style scoped>
.finance-manager {
  padding: 24px;
  background: linear-gradient(135deg, #e8f5e9 0%, #fffde7 100%);
  min-height: 100vh;
  position: relative;
}
.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
}
.page-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #388e3c;
  margin: 0 15px 0 0;
}
.operation-area {
  background: #f9fbe7;
  border-radius: 12px;
  box-shadow: 0 1px 4px rgba(103,194,58,0.08);
  margin-bottom: 24px;
}
.table-card {
  border-radius: 12px;
  box-shadow: 0 1px 4px rgba(103,194,58,0.08);
}
.operation-buttons {
  margin-top: 20px;
  display: flex;
  gap: 12px;
}
:deep(.el-table) {
  border-radius: 10px;
  overflow: hidden;
}
:deep(.el-table th) {
  background-color: #f0f4c3;
  color: #388e3c;
  font-weight: 500;
}
:deep(.el-table td) {
  padding: 12px 0;
}
:deep(.el-tag--success) {
  background: #e8f5e9;
  color: #43a047;
  border: none;
}
:deep(.el-tag--warning) {
  background: #fffde7;
  color: #bfa100;
  border: none;
}
:deep(.el-tag--danger) {
  background: #ffebee;
  color: #d32f2f;
  border: none;
}
.finance-decoration {
  position: absolute;
  right: 24px;
  bottom: 24px;
  width: 120px;
  opacity: 0.18;
  pointer-events: none;
  z-index: 0;
}
</style> 